package NIO;

import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Map;
import java.util.Set;

/**
 * @author VX5
 * @Title: MJC
 * @ProjectName DataStructure
 * @Description: TODO
 * @date ${DAT}10:06
 */
public class TestChannel {




    //字符集
    @Test
    public void test5(){
        Map<String, Charset> map = Charset.availableCharsets();

        Set<Map.Entry<String,Charset>> set = map.entrySet();

        for (Map.Entry<String, Charset> stringCharsetEntry : set) {
            System.out.println(stringCharsetEntry.getKey()+ "=" + stringCharsetEntry.getValue());
        }
    }

    @Test
    public void test6() throws CharacterCodingException {
        Charset cs1 = Charset.forName("GBK");

        //获取编码器与解码器
        CharsetEncoder encoder = cs1.newEncoder();

        //获取解码器
        CharsetDecoder decoder = cs1.newDecoder();

        CharBuffer charBuffer = CharBuffer.allocate(1024);
        charBuffer.put("阿里必过啊");
        charBuffer.flip();

        //编码
        ByteBuffer buffer = encoder.encode(charBuffer);

//        for (int i = 0; i < 12; i++){
//            System.out.println(buffer.get());
//        }

        //解码
        buffer.flip();
        CharBuffer charBuffer1 = decoder.decode(buffer);
        System.out.println(charBuffer.toString());

    }

/*-----------------------------------------------------------------------------------------------------*/
    @Test
    public void test3() throws IOException{
        FileChannel inChannel = FileChannel.open(Paths.get("C:\\Users\\VX5\\Desktop\\图片1.png"), StandardOpenOption.READ);
        FileChannel outChannel = FileChannel.open(Paths.get("C:\\Users\\VX5\\Desktop\\图片2.png"),StandardOpenOption.WRITE,StandardOpenOption.READ,StandardOpenOption.CREATE_NEW);

        //也是直接缓存区的方式
//        inChannel.transferTo(0,inChannel.size(),outChannel);
        outChannel.transferFrom(inChannel,0,inChannel.size());
        inChannel.close();
        outChannel.close();
    }

    //直接缓存区
    @Test
    public void test2() throws IOException {
        FileChannel inChannel = FileChannel.open(Paths.get("C:\\Users\\VX5\\Desktop\\图片1.png"), StandardOpenOption.READ);
        FileChannel outChannel = FileChannel.open(Paths.get("C:\\Users\\VX5\\Desktop\\图片3.png"),StandardOpenOption.WRITE,StandardOpenOption.READ,StandardOpenOption.CREATE_NEW);

        //内存映射文件
        MappedByteBuffer mapIn = inChannel.map(FileChannel.MapMode.READ_ONLY,0, inChannel.size());
        MappedByteBuffer mapOut = outChannel.map(FileChannel.MapMode.READ_WRITE,0, inChannel.size());

        //直接对缓冲区进行数据的读写操作
        byte[] dst = new byte[mapIn.limit()];
        mapIn.get(dst);
        mapOut.put(dst);

        inChannel.close();
        outChannel.close();

    }


    //非直接缓存区
    @Test
    public void test1() throws IOException {
        FileInputStream fis = new FileInputStream("C:\\Users\\VX5\\Desktop\\图片1.png");
        FileOutputStream fos = new FileOutputStream("C:\\Users\\VX5\\Desktop\\图片2.png");

        //获取通道
        FileChannel inChannel = fis.getChannel();
        FileChannel outChannel = fos.getChannel();

        //分配指定大小的缓存区
        ByteBuffer buffer = ByteBuffer.allocate(1024);

        //将通道中的数据存入缓冲区中
        while (inChannel.read(buffer) != -1){
            buffer.flip();
            //将缓存区中的数据写入通道中
            outChannel.write(buffer);
            buffer.clear();
        }

        outChannel.close();
        inChannel.close();
        fos.close();
        fis.close();
    }
}
